//
//  AppDelegate.m
//  ElectronicMall
//
//  Created by 史伟文 on 14/12/21.
//  Copyright (c) 2014年 cczu. All rights reserved.
//

#import "AppDelegate.h"
#import "EMRootViewController.h"
#import "EMAboutViewController.h"
#import "MBProgressHUD.h"
#import "EMAddToCartReqBody.h"
#import "EMAddToCartResoBody.h"
#import "UserInfo.h"
#import <ShareSDK/ShareSDK.h>
#import "WeiboSDK.h"
#import "WeiboApi.h"
#import "WXApi.h"
#import <TencentOpenAPI/QQApi.h>
#import <TencentOpenAPI/QQApiInterface.h>
#import <TencentOpenAPI/TencentOAuth.h>
#import <AlipaySDK/AlipaySDK.h>
#import "EMOrderSubmitViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
//    self.window.backgroundColor = [UIColor greenColor];
    
    [ShareSDK registerApp:@"62a35e275860"];

    [self initPlatform];
    
    self.window.rootViewController = [[EMRootViewController alloc] init];

    [self.window makeKeyAndVisible];
    return YES;
}

- (void)initPlatform
{
    [ShareSDK connectSinaWeiboWithAppKey:@"568898243"
                               appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3"
                             redirectUri:@"http://www.sharesdk.cn"];
    
    
    

    [ShareSDK connectTencentWeiboWithAppKey:@"801307650"
                                  appSecret:@"ae36f4ee3946e1cbb98d6965b0b2ff5c"
                                redirectUri:@"http://www.sharesdk.cn"
                                   wbApiCls:[WeiboApi class]];

    
    [ShareSDK connectQQWithQZoneAppKey:@"101174481"
                     qqApiInterfaceCls:[QQApiInterface class]
                       tencentOAuthCls:[TencentOAuth class]];
    
    [ShareSDK connectQZoneWithAppKey:@"101174481"
                           appSecret:@"4803ef3076ffa9f757fdb8f70e3aada2"
                   qqApiInterfaceCls:[QQApiInterface class]
                     tencentOAuthCls:[TencentOAuth class]];
    
    [ShareSDK connectWeChatWithAppId:@"wxc1db6c76d10827c3"
                           appSecret:@"981cc732629b2d83636206711c7ec95b"
                           wechatCls:[WXApi class]];
}

// alertView提醒视图（弹一个alert供用户操作）
- (void)showAlert:(NSString *)content
{
    EMAttention *attention = [[EMAttention alloc] initWithTitle:@"提示" contentText:content leftButtonTitle:nil rightButtonTitle:@"确定"];
    [attention show];
}

// textField消息提示（嘣，弹一条文字提示，一秒后消失）
- (void)showTip:(NSString *)content
{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.window animated:YES];
    
    // Configure for text only and offset down
    hud.mode = MBProgressHUDModeText;
    hud.labelText = content;
    hud.labelColor = [UIColor redColor];
    hud.margin = 10.f;
    hud.yOffset = -50.f;
    hud.removeFromSuperViewOnHide = YES;
    [hud hide:YES afterDelay:1.5];
}

// 页面中所有的加入购物车标志的动作
- (void)addToCartWithID:(NSString *)goodId
{
    [MBProgressHUD  showHUDAddedTo:self.window animated:YES];
    EMAddToCartReqBody *reqBody = [[EMAddToCartReqBody alloc] init];
    reqBody.JUDGEMETHOD = @"ADD-SHOPPING-CART";
    reqBody.ID = goodId;
    reqBody.NAME = [UserInfo sharedUserInfo].m_userName;
    reqBody.SESSION = [UserInfo sharedUserInfo].m_session;
    reqBody.NUM = @"1";
    HessianFormDataRequest *request = [[HessianFormDataRequest alloc] init];
    // 模型转字典
    request.postData = reqBody.keyValues;
    [request startRequestWithURL:baseURL andCompletionBlcok:^(NSDictionary *respInfo) {
        // 将返回的字典转成模型，键的名称必须相同
        EMAddToCartResoBody *respBody = [EMAddToCartResoBody objectWithKeyValues:respInfo];
        [MBProgressHUD hideHUDForView:self.window animated:YES];
        NSLog(@"%@", respInfo);
        if ([respBody.ERRORCODE isEqualToString: @"0000"]) {
            [self showTip:respBody.SUCCESSFULINFO];
        } else {
            [self showTip:respInfo[@"ERRORDESTRIPTION"]];
        }
        
    } andFailedBlock:^{
        
        [self showTip:@"无法连接服务器、请稍后再试"];
    }];

}

- (BOOL)application:(UIApplication *)application
      handleOpenURL:(NSURL *)url
{
    return [ShareSDK handleOpenURL:url
                        wxDelegate:self];
}

- (BOOL)application:(UIApplication *)application
            openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication
         annotation:(id)annotation
{
    if ([[url host] isEqualToString:@"safepay"]) {
        [EMOrderSubmitViewController callBackActionWithURL:url];
        return YES;
    } else {
        return [ShareSDK handleOpenURL: url
                 sourceApplication:sourceApplication
                        annotation: annotation
                        wxDelegate: self];
    }
}

#pragma mark - WXApiDelegate(optional)
-(void) onReq:(BaseReq*)req
{}

-(void) onResp:(BaseResp*)resp
{}


@end
